<template>
  <el-dialog
    v-if="open"
    class="dia-style"
    :title="title"
    :visible.sync="open"
    width="1000px"
    :close-on-click-modal="false"
    @close="cancel"
  >
    <el-form ref="form" :model="formData" label-width="140px" label-position="left" style="padding: 0 10px">
      <!-- 基础信息部分 -->
      <div class="upload-title-box">
        <div class="upload-title">基础信息</div>
        <el-checkbox v-model="showRequired" @change="changeShow">仅展示必填信息 </el-checkbox>
      </div>
      <HkForm
        ref="hkFormRef1"
        :form-labels="basicArr"
        :rules="formRules"
        :data="formData"
        :dict-list="dictList"
        :btn-hidden="true"
        label-width="140"
        label-position="left"
      >
        <template #filter-orgRegion>
          <el-cascader
            v-model="formData.orgRegion"
            :options="dictList.zoneList"
            :props="casProps"
            :filterable="true"
            placeholder="请选择所属区域"
            @change="handleChange"
          />
        </template>

        <template #filter-parentId>
          <treeselect
            v-model="formData.parentId"
            :options="dictList.orgList"
            :normalizer="normalizer"
            :append-to-body="true"
            placeholder="选择上级部门"
            noResultsText="无匹配数据" 
          />
        </template>
      </HkForm>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">保 存</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import HkForm from '@/components/Hk-Form'
import Treeselect from '@riophae/vue-treeselect'
import { listAll, addDatas, editDatas, queryById, queryByOrder } from '@/api/assets/org.js'
import AddTable from '../../../../components/Common/addTable.vue'
import { cloneDeep } from '../../../../utils/loadash-es'

export default {
  components: { HkForm, AddTable, Treeselect },
  props: {
    // 标题
    title: {
      type: String,
      default: ''
    },
    treeId: {
      type: String,
      default: ''
    },
    dictList: {
      type: Object,
      default: () => {}
    }
  },
  data() {
    return {
      // 对话框是否显示
      open: false,
      // 仅展示必填信息
      showRequired: true,
      // 折叠菜单展示的数据
      activeNames: ['1', '2', '3', '4', '5'],
      casProps: { expandTrigger: 'hover', value: 'zoneCode', label: 'zoneName', children: 'children' },
      // 表单数据
      formData: {},
      // 基础信息
      basicArr: [
        {
          label: '部门编号',
          field: 'orgNo',
          type: 'input',
          maxlength: 20
        },
        {
          label: '部门名称',
          field: 'name',
          type: 'input',
          maxlength: 32
        },
        {
          label: '上级部门',
          field: 'parentId',
          type: 'slot',
          hidden: false

        },
        {
          label: '机构类型',
          field: 'orgType',
          type: 'select',
          list: 'orgTypeList'
        },
        {
          label: '单位类别',
          field: 'unitCategory',
          type: 'select',
          list: 'unitCategoryList'
        },
        {
          label: '保密资质',
          field: 'secQualifications',
          type: 'select',
          list: 'secQualificatList'
        },
        {
          label: '负 责 人',
          field: 'personLiable',
          type: 'input',
          hidden: false,
          maxlength: 32
        },

        {
          label: '联系电话',
          field: 'telephone',
          type: 'input',
          hidden: false,
          maxlength: 32
        },
        {
          label: '邮箱地址',
          field: 'email',
          type: 'input',
          hidden: false,
          maxlength: 32
        },
        {
          label: '机构排序',
          field: 'orders',
          type: 'number',
          hidden: false
        },
        {
          label: '所属区域',
          field: 'orgRegion',
          type: 'slot',
          hidden: false
        },

        {
          label: '详细地址信息',
          field: 'address',
          type: 'input',
          hidden: false,
          maxlength: 300
        },

        {
          label: '备注',
          field: 'remark',
          type: 'textarea',
          hidden: false,
          maxlength: 500,
          attrs: {
            rows: '8'
          },
          span: 24
        }
      ],
      // 表单校验
      formRules: {
        name: [{ required: true, message: '不能为空', trigger: 'blur' }],
        // parentId: [{ required: true, message: '不能为空', trigger: 'blur' }],
        orgNo: [{ required: true, message: '不能为空', trigger: 'blur' }],
        orgType: [{ required: true, message: '不能为空', trigger: 'blur' }],
        secQualifications: [{ required: true, message: '不能为空', trigger: 'blur' }],
        unitCategory: [{ required: true, message: '不能为空', trigger: 'blur' }]
      },

      // 表格展示的数据
      tableData: [],
      // 表格列表的结构
      tableList: [
        { label: 'IP地址', param: 'equipmentUserId', type: 'input' },
        { label: 'MAC', param: 'equipmentOrgId', type: 'input' }
      ]
    }
  },
  computed: {},
  watch: {},

  mounted() {},
  methods: {
    handleChange(data) {},
    /** 转换部门数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children
      }
      return {
        id: node.id,
        label: node.name,
        children: node.children
      }
    },
    // 是否只展示必填项
    changeShow() {
      this.basicArr.forEach((item) => {
        if (typeof item.hidden === 'boolean') {
          item.hidden = this.showRequired
        }
      })
    },
    // 清空表单
    resetFields() {
      this.$refs['form'].resetFields()
      this.$refs['hkFormRef1']?.resetForm('formLabels')
    },
    show(id) {
      if (!id) {
        this.formData = {}
      }
      queryByOrder(id).then((res) => {
        const size = res + 1
        const num = 100000 + size
        this.$set(this.formData, 'orders', size)
        this.$set(this.formData, 'orgNo', num)
      })
      this.showRequired = true
      this.changeShow()
      if (this.treeId) {
        this.formData.parentId = this.treeId
      }
      return new Promise((resolve, reject) => {
        if (id) {
          queryById(id).then((res) => {
            this.formData = res
            if (this.formData.parentId == 0) {
              this.basicArr.forEach((item) => {
                if (item.field == 'parentId') {
                  item.hidden = true
                }
              })
            } else {
              this.basicArr.forEach((item) => {
                if (item.field == 'parentId') {
                  item.hidden = true
                }
              })
            }
          })
        }
        resolve(this.formData)
      }).then((res) => {
        this.open = true
      })
    },
    // 提交事件
    async submitForm() {
      //  let res = await this.$refs.add_table.submitTable();

      const res = await this.$refs.hkFormRef1.handleFormSubmit('formLabels')
      if (this.title == '添加') {
        addDatas(this.formData).then((res) => {
          this.$message.success('新增成功')
          this.open = false
          this.$emit('handleAddOff')
        })
      } else {
        editDatas(this.formData).then((res) => {
          this.$message.success('修改成功')
          this.open = false
          this.$emit('handleAddOff')
        })
      }
    },
    // 关闭按钮
    cancel() {
      this.formData = {}
      this.open = false
    }
  }
}
</script>

<style lang="scss" scoped>
.upload-title-box {
  height: 60px;
  width: 100%;
  padding: 0 10px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  border-bottom: 1px solid rgba($color: #000000, $alpha: 8%);

  .upload-title {
    font-size: 16px;
    font-weight: 700;
    color: #000000;
  }
}

::v-deep {
  .el-collapse-item {
    border-bottom: 1px solid #e6ebf5;

    .el-collapse-item__wrap {
      border-bottom: none;
    }

    .el-collapse-item__content {
      padding-bottom: 0px;
    }
  }

  .iRow {
    margin: 0 !important;
  }

  .el-dialog__body {
    display: flex;
    flex-direction: column;
  }

  .el-collapse {
    border-top: none;

    .el-collapse-item__header {
      height: 60px;
      font-size: 16px;
      font-weight: 700;
      color: #000000;
      margin-left: 30px;
      border-bottom: none;
    }

    .el-collapse-item__arrow {
      margin: 0 auto 0 10px;
      margin-left: -90px;
    }
  }
}

::v-deep .el-cascader {
  width: 100%;
}
</style>
